package com.zhou;

import java.util.LinkedList;
import java.util.Scanner;

/**
 * T15
 *
 * @author zhouxianfeng
 * @date 2021-8-24
 */
public class T15 {
    //1.表示0
//2.#表示1
//3.S我的启示位置 2
//4.0表示箱子的位置，3
//5.E目标位置，表示 4


    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int row = scanner.nextInt();
        int col = scanner.nextInt();
        scanner.nextLine();
        int px = 0, py = 0, bx = 0, by = 0;

        char[][] mm = new char[row][col];
        for (int i = 0; i < row; i++) {
            String s = scanner.nextLine();
            for (int j = 0; j < col; j++) {
                mm[i][j] = s.charAt(j);
                if (mm[i][j] == 'S') {
                    px = i;
                    py = j;
                } else if (mm[i][j] == '0') {
                    bx = i;
                    by = j;
                }
            }
        }
        System.out.println(res(mm, px, py, bx, by));


    }

    private static int res(char[][] mm, int px, int py, int bx, int by) {
        int x = mm.length;
        int y = mm[0].length;
        int[][][][] go = new int[x][y][x][y];
        //上下左右移动
        int[][] move = new int[][]{{1, 0}, {-1, 0}, {0, -1}, {0, 1}};

        LinkedList<Node> queue = new LinkedList();
        Node start = new Node(px, py, bx, by);
        go[px][py][bx][by] = 1;
        queue.add(start);
        while (!queue.isEmpty()) {
            Node curr = queue.poll();
            int currBx = curr.bx;
            int currBy = curr.by;
            for (int[] mv : move) {
                if (curr.px == curr.bx) {
                    if (curr.py + mv[1] == curr.by) {
                        currBy = curr.by + mv[1];
                    }
                }
                if (curr.py == curr.by) {
                    if (curr.px + mv[0] == curr.bx) {
                        currBx = curr.bx + mv[0];
                    }
                }

                Node next = new Node(curr.px + mv[0], curr.py + mv[1], currBx, currBy);
                next.step = curr.step + 1;
                if (next.px < 0 || next.px >= x || next.py < 0 || next.py >= y || mm[next.px][next.py] == '#' ||
                        next.bx < 0 || next.bx >= x || next.by < 0 || next.by >= y || mm[next.bx][next.by] == '#') {
                    continue;
                }
                if (go[next.px][next.py][next.bx][next.by] == 0) {
                    go[next.px][next.py][next.bx][next.by] = 1;
                } else {
                    continue;
                }
                if (mm[next.bx][next.by] == 'E') {
                    return next.step;
                }
                queue.add(next);
            }
        }
        return -1;
    }


    public static class Node {
        int px;
        int py;
        int bx;
        int by;
        int step;

        public Node(int px, int py, int bx, int by) {
            this.px = px;
            this.py = py;
            this.bx = bx;
            this.by = by;
        }
    }

}
